home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / progtool / c / gcc / gempp19.zoo / gem++19 / src / gemst.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-21  |  3.6 KB  |  176 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  This file is Copyright 1993 by Warwick W. Allison.
  4. //  This file is part of the gem++ library.
  5. //  You are free to copy and modify these sources, provided you acknowledge
  6. //  the origin by retaining this notice, and adhere to the conditions
  7. //  described in the file COPYING.LIB.
  8. //
  9. /////////////////////////////////////////////////////////////////////////////
  10.  
  11. #include "gemst.h"
  12. #include "gemfn.h"
  13. #include "grect.h"
  14. #include "vdi.h"
  15. #include <string.h>
  16.  
  17. template <class T>
  18. void swap(T& a, T& b)
  19. {
  20.     T temp=a;
  21.     a=b;
  22.     b=temp;
  23. }
  24.  
  25.  
  26. GEMscrolltext::GEMscrolltext(GEMform& form, int RSCindex, int width, int height) :
  27.     GEMscrollableobject(form,RSCindex),
  28.     w(width),h(height),
  29.     textline(new char*[height]),
  30.     top_aligned(FALSE)
  31. {
  32.     change.Clear();
  33.  
  34.     for (int j=0; j<h; j++) {
  35.         textline[j]=new char[w+1]; // +1 for '\0' terminator.
  36.         for (int i=0; i<w; i++) {
  37.             textline[j][i]='*';
  38.         }
  39.     }
  40. }
  41.  
  42. GEMscrolltext::~GEMscrolltext()
  43. {
  44.     for (int j=0; j<h; j++)
  45.         delete textline[j];
  46.  
  47.     delete textline;
  48. }
  49.  
  50. // These are used for passing font size from ScrollText and Draw
  51. // to RedrawClipped, through Scroll.
  52. static int global_fontwidth;
  53. static int global_fontheight;
  54.  
  55. void GEMscrolltext::ScrollText(int columns_right, int lines_down)
  56. {
  57.     // Flush any changes.
  58.     // (could optimize by unchanging area about to be scrolled away)
  59.     Refresh();
  60.  
  61.     if (columns_right>0) {
  62.         if (columns_right>=w) {
  63.             // Just clear it all.
  64.             for (int j=0; j<h; j++) {
  65.                 memset(textline[j],' ',w);
  66.             }
  67.         } else {
  68.             for (int j=0; j<h; j++) {
  69.                 memcpy(textline[j],textline[j]+columns_right,w-columns_right);
  70.                 memset(textline[j]+w-columns_right,' ',columns_right);
  71.             }
  72.         }
  73.     } else if (columns_right<0) {
  74.         if (-columns_right>=w) {
  75.             // Just clear it all.
  76.             for (int j=0; j<h; j++) {
  77.                 memset(textline[j],' ',w);
  78.             }
  79.         } else {
  80.             for (int j=0; j<h; j++) {
  81.                 memcpy(textline[j]-columns_right,textline[j],w+columns_right);
  82.                 memset(textline[j],' ',-columns_right);
  83.             }
  84.         }
  85.     }
  86.  
  87.     if (lines_down>0) {
  88.         for (int j=0; j<h-lines_down; j++) {
  89.             swap(textline[j],textline[j+lines_down]);
  90.         }
  91.         for (int jj=h-1; jj>0 && jj>j; j--) {
  92.             memset(textline[jj],' ',w);
  93.         }
  94.     } else if (lines_down<0) {
  95.         for (int j=h-1; j>=-lines_down; j--) {
  96.             swap(textline[j],textline[j+lines_down]);
  97.         }
  98.         for (j=j; j>=0; j--) {
  99.             memset(textline[j],' ',w);
  100.         }
  101.     }
  102.  
  103.     SetRedrawFont();
  104.  
  105.     Scroll(columns_right*global_fontwidth,lines_down*global_fontheight);
  106. }
  107.  
  108. void GEMscrolltext::SetRedrawFont()
  109. {
  110.     global_fontwidth=CharCellWidth();
  111.     global_fontheight=CharCellHeight();
  112.  
  113.     if (top_aligned) {
  114.         int j;
  115.         st_alignment(0,5,&j,&j); // left-top
  116.     } else {
  117.         int j;
  118.         st_alignment(0,3,&j,&j); // left-bottom
  119.     }
  120. }
  121.  
  122. void GEMscrolltext::RedrawClipped(int x, int y, const GRect& area)
  123. {
  124.     clip(area.g_x,area.g_y,area.g_x+area.g_w-1,area.g_y+area.g_h-1);
  125.  
  126.     if (top_aligned) {
  127.         int j=0;
  128.         while (y < area.g_y+area.g_w && j<h) {
  129.             gtext(x,y,textline[j]);
  130.             y+=global_fontheight;
  131.             j++; // 
  132.         }
  133.     } else {
  134.         y+=GEMuserobject::Height();
  135.         int j=h-1;
  136.         while (y >= area.g_y && j>=0) {
  137.             gtext(x,y,textline[j]);
  138.             y-=global_fontheight;
  139.             j--;
  140.         }
  141.     }
  142.  
  143.     clip_off();
  144. }
  145.  
  146. void GEMscrolltext::Put(char ch, int x, int y)
  147. {
  148.     textline[y][x]=ch;
  149.     change.Include(x,y);
  150. }
  151.  
  152. void GEMscrolltext::Refresh()
  153. {
  154.     if (change.Changed()) {
  155.         int fontwidth=CharCellWidth();
  156.         int fontheight=CharCellHeight();
  157.  
  158.         change.Scale(fontwidth,fontheight);
  159.  
  160.         if (!top_aligned) {
  161.             change.g_x+=GEMuserobject::Height()-fontheight*h;
  162.         }
  163.  
  164.         form.RedrawObject(myindex,
  165.             change.g_x,change.g_y,change.g_w,change.g_h);
  166.  
  167.         change.Clear();
  168.     }
  169. }
  170.  
  171. void GEMscrolltext::Draw(const PARMBLK* pb)
  172. {
  173.     SetRedrawFont();
  174.     GEMscrollableobject::Draw(pb);
  175. }
  176.